What is ethers?
The ethers npm package is a library that provides a set of tools to interact with the Ethereum blockchain. It allows users to connect to the Ethereum network, manage wallets and keys, interact with smart contracts, and perform various other blockchain-related operations.
What are ethers's main functionalities?
Connecting to Ethereum Network
This code sample demonstrates how to connect to the Ethereum network using ethers with an Infura provider.
const { ethers } = require('ethers');
const provider = new ethers.providers.JsonRpcProvider('https://mainnet.infura.io/v3/YOUR_INFURA_API_KEY');
Wallet Management
This code sample shows how to create a new wallet from a private key and connect it to an Ethereum provider.
const { ethers } = require('ethers');
const wallet = new ethers.Wallet('YOUR_PRIVATE_KEY');
const connectedWallet = wallet.connect(provider);
Interacting with Smart Contracts
This code sample illustrates how to interact with a smart contract by creating a contract instance and calling one of its functions.
const { ethers } = require('ethers');
const abi = [...] // Contract ABI
const contractAddress = '0x...'; // Contract address
const contract = new ethers.Contract(contractAddress, abi, provider);
const value = await contract.someFunction();
Sending Transactions
This code sample demonstrates how to send a transaction to the Ethereum network using a wallet instance.
const { ethers } = require('ethers');
const tx = {
to: '0x...',
value: ethers.utils.parseEther('1.0'),
gasLimit: 21000,
gasPrice: ethers.utils.parseUnits('10', 'gwei')
};
const sendPromise = wallet.sendTransaction(tx);
Querying Blockchain Data
This code sample shows how to query blockchain data, such as retrieving the latest block number and details of a specific block.
const { ethers } = require('ethers');
const blockNumber = await provider.getBlockNumber();
const block = await provider.getBlock(blockNumber);
Other packages similar to ethers
truffle-contract
Truffle Contract is part of the Truffle Suite and is designed to provide a more comfortable abstraction for interacting with Ethereum smart contracts. It is often used in combination with other Truffle tools for development, testing, and deployment. While it offers similar contract interaction capabilities, it is more tightly integrated with the Truffle development environment compared to ethers.
drizzle
Drizzle is a collection of front-end libraries that make writing dApp front-ends easier and more predictable. It is part of the Truffle Suite and is designed to work with a Redux store. Drizzle provides reactive contract data fetching and transaction processing, which can be more convenient for dApp development. However, it is more opinionated and specific to front-end development compared to ethers, which is more general-purpose.
ethers.js
Complete Ethereum wallet implementation and utilities in JavaScript.
Features:
- Keep your private keys in your client, safe and sound
- Import and export JSON wallets (Geth, Parity and crowdsale) and brain wallets
- Import and export BIP 39 mnemonic phrases (12 word backup phrases) and HD Wallets
- Meta-classes create JavaScript objects from any contract ABI
- Connect to Ethereum nodes over JSON-RPC, INFURA, Etherscan, or MetaMask
- ENS names are first-class citizens; they can almost always used instead of Ethereum addresses
- Tiny (~79kb compressed; 242kb uncompressed)
- Complete functionality for all your Ethereum needs
- Extensive documentation
- Large collection of test cases which are maintained and added to
- MIT License (including ALL dependencies); completely open source to do with as you please
Installing
To use in a browser:
<script charset="utf-8"
src="https://cdn.ethers.io/scripts/ethers-v3.min.js"
type="text/javascript">
</script>
To use in node.js:
/Users/ethers/my-app> npm install --save ethers
Documentation
Browse the API Documentation online.
Documentation is generated using Sphinx and can be browsed locally from the /docs/build/html directory.
Donations
I do this because I love it, but if you want to buy me a coffee, I won't say no. :o)
Ethereum: 0xEA517D5a070e6705Cc5467858681Ed953d285Eb9
License
Completely MIT Licensed. Including ALL dependencies.